local pointsMarkers={
	{1,-1954.42798, 280.21780, 35.46875},
	{2,1058.57,2368.92,10.82},
	{3,525.44,-1299.12,17.20},
}

local pointsVehMarkers={
	{-1989.65, 276.34, 34.80,0,0,266},
	{1005.37,2446.73,10.25,0.31,0.00,179.84},
	{561.15,-1288.99,16.66,0.00,0.00,359.26},
}

for i,v in ipairs(pointsMarkers) do
	local marker=createMarker(v[2], v[3], v[4], "cylinder", 2, 100, 0, 255,0)
    setElementData(marker,"salon_marker:id",tonumber(v[1]))
    setElementData(marker, 'marker:data', {name='E-Salon', icon='dealer',size=1})
    createBlip(v[2], v[3], v[4],38)
end

VehiclesList = {

    [1] = { -- 5k-
        [1] = "418",
        [2] = "478",
        [3] = "410",
        [4] = "404",
        [5] = "492",
        [6] = "422",
        [7] = "500",
        [8] = "401",
        [9] = "436",
        [10] = "466",
        [11] = "554",
        [12] = "540",
    };
    [2] = { -- 5-10k
        [1] = "546",
        [2] = "543",
        [3] = "527",
        [4] = "496",
        [5] = "551",
        [6] = "585",
        [7] = "600",
        [8] = "529",
        [9] = "458",
        [10] = "526",
        [11] = "549",
        [12] = "517",
        [13] = "405",
        [14] = "445",
        [15] = "566",
        [16] = "507",
        [17] = "516",
        [18] = "555",
        [19] = "400",
        [20] = "602",
        [21] = "542",
        [22] = "589",
        [23] = "579",
        [24] = "439",
        [25] = "587",
        [26] = "426",
        [27] = "421",
        [28] = "559",
    };
    [3] = { -- 10-20k
        [1] = "562",
        [2] = "477",
        [3] = "480",
        [4] = "402",
        [5] = "506",
        [6] = "603",
        [7] = "415",
        [8] = "560",
    };
    [4] = { -- 20k+
        [1] = "558",
        [2] = "565",
        [3] = "451",
        [4] = "429",
        [5] = "541",
        [6] = "411",
    };
}

function hit(el,md)
    if not md or getElementType(el) ~= "player" then return end
    if isPedInVehicle(el) then return end
	local code=getElementData(source,"salon_marker:id")
	if not code then return end
    triggerClientEvent(el,'cmta_salon:showGUI',resourceRoot)
    setElementFrozen(el,true)
    setElementData(el,"player:salonID",code)
end
addEventHandler( "onMarkerHit", resourceRoot, hit)

function leave(el,md)
    if not md or getElementType(el) ~= "player" then return end
    triggerClientEvent(el,'cmta_salon:hideGUI',resourceRoot)
    setElementData(el,"player:salonID",nil)
end
addEventHandler( "onMarkerLeave", resourceRoot, leave)

addEvent("cmta_salon:sgetvehs", true)
addEventHandler("cmta_salon:sgetvehs", root, function()
    local r=exports["cmta-db"]:dbGet("select * from cmta_carshowroom ORDER BY name ASC")
    triggerClientEvent(client,"cmta_salon:getvehs",resourceRoot, r)
end)

addEvent("cmta_salon:infoveh", true)
addEventHandler("cmta_salon:infoveh", root, function(name)
    local r=exports["cmta-db"]:dbGet("select * from cmta_carshowroom WHERE name=?",name)
    triggerClientEvent(client,"cmta_salon:info",resourceRoot, r)
end)

addEvent("cmta_salon:testdrive", true)
addEventHandler("cmta_salon:testdrive", getRootElement(), function(id)
    triggerClientEvent(client,'cmta_salon:hideGUI',resourceRoot)
    setElementData(client,"testdrive:dimension",getElementDimension(client))
    x,y,z = getElementPosition(client)
    setElementDimension(client,getElementData(client,"player:data").uid)
    if getElementData(client,"player:salonID") == 1 then
    vehicle = createVehicle(getVehicleModelFromName(id), pointsVehMarkers[1][1], pointsVehMarkers[1][2], pointsVehMarkers[1][3],pointsVehMarkers[1][4],pointsVehMarkers[1][5],pointsVehMarkers[1][6])
    elseif getElementData(client,"player:salonID") == 2 then
        vehicle = createVehicle(getVehicleModelFromName(id), pointsVehMarkers[2][1], pointsVehMarkers[2][2], pointsVehMarkers[2][3],pointsVehMarkers[2][4],pointsVehMarkers[2][5],pointsVehMarkers[2][6])
    elseif getElementData(client,"player:salonID") == 3 then
        vehicle = createVehicle(getVehicleModelFromName(id), pointsVehMarkers[3][1], pointsVehMarkers[3][2], pointsVehMarkers[3][3],pointsVehMarkers[3][4],pointsVehMarkers[3][5],pointsVehMarkers[3][6])
    end
    setElementData(vehicle,"vehicle:testsalon",true)
    local fast = getVehicleHandlingProperty(vehicle,"engineAcceleration")
    setVehicleHandling(vehicle,"engineAcceleration",fast -1)
    setElementDimension(vehicle,getElementData(client,"player:data").uid)
    setElementData(client,"testdrive:owner",vehicle)
    warpPedIntoVehicle(client,vehicle)
    outputChatBox("Aby zakończyć jazdę próbną #ff0000wysiądź z pojazdu!",client,255,255,255,true)
end)

addEvent("cmta_salon:buy", true)
addEventHandler("cmta_salon:buy", root, function(name,engine,color1,color2,color3)
    local r=exports["cmta-db"]:dbGet("select * from cmta_carshowroom WHERE name=?",name)
    if r[1]["pcs"] < 1 then outputChatBox("#ff2828Zabrakło pojazdów tego modelu, poczekaj na dostawę!",client,255,255,255,true) triggerClientEvent(client,'cmta_salon:hideGUI',resourceRoot)        return end
    if getElementData(client,"player:data").points < r[1]["price"] then outputChatBox("#ff2828Nie posiadasz wystarczającej ilości punktów, aby zakupić ten pojazd.",client,255,255,255,true) return end
    local data = getElementData(client,"player:data")
    data.points = data.points-r[1]["price"]
    setElementData(client,"player:data",data)
    exports["cmta-db"]:dbSet("update cmta_carshowroom set buyers=buyers+1,pcs=pcs-1 where name=?",name)
    if engine == 1 then resultengine = "Benzyna" resultamount = "1.0" else resultengine = "Diesel" resultamount = "1.1" end
    exports["cmta-db"]:dbSet("INSERT INTO cmta_pojazdy (model,engine,driveType,engineType,ownedPlayer,mileage,parking,color) VALUES (?,?,?,?,?,0,1,?)", r[1]["id"],resultamount,"FWD",resultengine, getElementData(client,"player:data").uid, ''..color1..','..color2..','..color3..'' )
    exports["cmta-db"]:dbSet("INSERT INTO cmta_logs_vehicles SET vehid=?, nameowner=?, idowner=?, operation=?","0",getPlayerName(client):gsub("#%x%x%x%x%x%x",""),getElementData(client,"player:data").uid,"Kupno "..name.." z salonu za "..r[1]["price"].." pkt")
    if not exports["cmta-osiagniecia"]:hasPlayerAchievement(client, "Pierwsze auto") then
        exports["cmta-osiagniecia"]:addAchievement(client, "Pierwsze auto")
    end
    outputChatBox("#808080Pomyślnie zakupiono pojazd #ff2828"..name.." #808080za #ff2828"..r[1]["price"].." pkt.",client,255,255,255,true)
    triggerClientEvent(client,'cmta_salon:hideGUI',resourceRoot)



    local r=exports["cmta-db"]:dbGet("SELECT price,SUM(pcs) from cmta_carshowroom")
    for i,v in pairs(r) do
    if tonumber(v["price"]) < 5000 and tonumber(v["pcs"]) < 15 then
        for i=1,10 do
            local randomCar = math.random(1,12)
            exports["cmta-db"]:dbSet("update cmta_carshowroom set pcs=pcs+1 where id=?",VehiclesList[1][randomCar])
            outputChatBox("dodaneauto",client)
        end
    end
    if tonumber(v["price"]) >= 5000 and tonumber(v["price"]) < 10000 and tonumber(v["pcs"]) < 10 then
        for i=1,8 do
            local randomCar = math.random(1,28)
            exports["cmta-db"]:dbSet("update cmta_carshowroom set pcs=pcs+1 where id=?",VehiclesList[2][randomCar])
        end
    end
    if tonumber(v["price"]) >= 10000 and tonumber(v["price"]) < 20000 and tonumber(v["pcs"]) < 6 then
        for i=1,4 do
            local randomCar = math.random(1,8)
            exports["cmta-db"]:dbSet("update cmta_carshowroom set pcs=pcs+1 where id=?",VehiclesList[3][randomCar])
        end
    end
    if tonumber(v["price"]) >= 20000 and tonumber(v["pcs"]) < 4 then
        for i=1,2 do
            local randomCar = math.random(1,6)
            exports["cmta-db"]:dbSet("update cmta_carshowroom set pcs=pcs+1 where id=?",VehiclesList[4][randomCar])
        end
    end



end


end)

addEventHandler("onVehicleStartExit",getRootElement(),function(plr,seat)
    if getElementData(source,"vehicle:testsalon") then 
	local veh = getElementData(plr,"testdrive:owner")
	destroyElement(veh)
    setTimer(function()
	setElementDimension(plr,getElementData(plr,"testdrive:dimension"))
	setElementPosition(plr,x,y,z)
	end,1000,1)
	return end
end)

function quitPlayer ( )
    if isElement(getElementData(source,"testdrive:owner")) then
        destroyElement(getElementData(source,"testdrive:owner"))
    end
end
addEventHandler ( "onPlayerQuit", root, quitPlayer )

function getVehicleHandlingProperty ( element, property )
    if isElement ( element ) and getElementType ( element ) == "vehicle" and type ( property ) == "string" then
        local handlingTable = getVehicleHandling ( element )
        local value = handlingTable[property]
        if value then
            return value
        end
    end
    return false
end